## Warning: package 'BiocGenerics' was built under R version 4.0.5
## Warning: package 'GenomeInfoDb' was built under R version 4.0.5
load("~/Documents/MiGASti/Databases/gene_matrix.RData")
metadata <- read.table("~/Documents/MiGASti/Databases/metadata2.txt")
#set rownames to Sample
row.names(metadata) <- metadata$Sample 
setwd("~/Documents/MiGASti/Databases")
#exclude samples that did not pass QC filtering
exclude <- read.table("samples2remove2.txt")
exclude <- exclude$x
genes_counts_filt = genes_counts[, !colnames(genes_counts) %in% exclude] 
#Excludes the samples from filters. 
#dim(genes_counts_filt)
metadata_filt = metadata[ !(rownames(metadata) %in% exclude), ]
length(metadata_filt)
gencode_30 = read.table("~/Documents/MiGASti/Databases/ens.geneid.gencode.v30")
colnames(gencode_30) = c("ensembl","symbol")
#order metadata and genes counts
genes_counts_ordered <- genes_counts_filt[,rownames(metadata_filt)]
#head(genes_counts_ordered)
all(rownames(metadata_filt) == colnames (genes_counts_ordered)) #TRUE

Preparing the samples for DEG

#remove uncultured samples
metadata_cultured <- metadata_filt[metadata_filt$Stimulation != "ununstim",]
metadata_cultured_subset <- metadata_cultured[metadata_cultured$Stimulation != "TNFa",]
#check numbers
#dim(metadata_cultured)
#check numbers per stimulation
#table(metadata_filt$Stimulation)
#select only GTS samples in metadata
metadata_GTS = metadata_cultured_subset[metadata_cultured_subset$Region=="GTS",]
#select only GTS samples in genes counts
genes_counts_GTS <- genes_counts_ordered[,metadata_GTS$Sample]
#order metadata and genes_counts
genes_counts_GTS_ordered <- genes_counts_GTS[,rownames(metadata_GTS)]
#check ordering
all(rownames(metadata_GTS) == colnames (genes_counts_GTS_ordered)) #TRUE

[1] TRUE

#round counts; deseq2 can only handle integers
genes_counts_GTS_ordered <- round(genes_counts_GTS_ordered, digits=0)

#make sure covariate variables are the right format 
#cannot create dds object with numeric values
metadata_GTS$Donor_id <- as.factor(metadata_GTS$Donor_id)
metadata_GTS$age <- as.integer(metadata_GTS$age)
metadata_GTS$sex <- as.factor(metadata_GTS$sex)
metadata_GTS$Stimulation <- as.factor(metadata_GTS$Stimulation)
metadata_GTS$picard_pct_ribosomal_bases = scale(metadata_GTS$picard_pct_ribosomal_bases)
metadata_GTS$picard_pct_mrna_bases = scale(metadata_GTS$picard_pct_mrna_bases)
metadata_GTS$picard_pct_pf_reads_aligned = scale(metadata_GTS$picard_pct_pf_reads_aligned)
metadata_GTS$picard_pct_percent_duplication = scale(metadata_GTS$picard_pct_percent_duplication)

#adjust for: ~ age + (1|donor_id) + picard_pct_ribosomal_bases + picard_pct_mrna_bases +   picard_pct_percent_duplication + picard_pct_pf_reads_aligned 
table(metadata_GTS$Stimulation)

IFNy LPS R848 unstim 12 25 1 27

DESeq2 of GTS samples

#createDeSEQ2 object for LPS
dds <- DESeqDataSetFromMatrix(countData = genes_counts_GTS_ordered,
                              colData = metadata_GTS,
                              design = ~ age + sex + picard_pct_ribosomal_bases + picard_pct_mrna_bases + picard_pct_percent_duplication + picard_pct_pf_reads_aligned + Stimulation) 
#variable of interest at end of the formula

#Make sure that control group is set as the reference group
dds$Stimulation <- relevel(dds$Stimulation, ref="unstim")
#head(dds)

#filter: CPM > 1 in 50% of the samples 
keep.exp = rowSums(cpm(genes_counts_GTS_ordered) > 1) >= 0.5*ncol(genes_counts_GTS_ordered)
dds = dds[keep.exp,]

#Run differential expression 
dds <- DESeq(dds, betaPrior = FALSE)
resultsNames(dds)

[1] “Intercept” “age”
[3] “sex_m_vs_f” “picard_pct_ribosomal_bases”
[5] “picard_pct_mrna_bases” “picard_pct_percent_duplication” [7] “picard_pct_pf_reads_aligned” “Stimulation_IFNy_vs_unstim”
[9] “Stimulation_LPS_vs_unstim” “Stimulation_R848_vs_unstim”

DESeq2: LPS vs unstim

Number of differentially expressed genes

# generate results table for LPS vs unstim
res_LPS <- results(dds, name="Stimulation_LPS_vs_unstim")
sum(res_LPS$padj < 0.05, na.rm=TRUE)

[1] 44

resOrdered_LPS <- res_LPS[order(res_LPS$pvalue),] 
resOrdered_LPS <- as.data.frame(resOrdered_LPS)

Volcano plot LPS vs unstim

head(res_LPS)

log2 fold change (MLE): Stimulation LPS vs unstim Wald test p-value: Stimulation LPS vs unstim DataFrame with 6 rows and 6 columns baseMean log2FoldChange lfcSE stat pvalue ENSG00000000003.14 21.3789 0.378953 0.322991 1.173263 0.240690 ENSG00000000419.12 216.0836 -0.179567 0.237569 -0.755851 0.449739 ENSG00000000457.14 98.3586 0.203929 0.205746 0.991168 0.321604 ENSG00000000460.17 29.1474 -0.222071 0.244093 -0.909778 0.362939 ENSG00000000938.13 583.1242 0.170174 0.249829 0.681161 0.495770 ENSG00000000971.15 48.0277 0.200629 0.287863 0.696960 0.485828 padj ENSG00000000003.14 NA ENSG00000000419.12 0.824143 ENSG00000000457.14 NA ENSG00000000460.17 NA ENSG00000000938.13 0.844743 ENSG00000000971.15 NA

with(res_LPS, plot(log2FoldChange, -log10(pvalue), pch=20, main="Volcano plot", xlim=c(-2.5,2)))
with(subset(res_LPS, padj<.05 ), points(log2FoldChange, -log10(pvalue), pch=20, col="red"))

MA plot LPS vs unstim

#The function plotMA shows the log2 fold changes attributable to a given variable over the mean of normalized counts for all the samples in the DESeqDataSet. Points will be colored if the adjusted p value is less than 0.1. Points which fall out of the window are plotted as open triangles pointing either up or down.

plotMA(res_LPS, ylim=c(-2,2))

TOP differentially expressed genes LPS vs unstim

setDT(resOrdered_LPS, keep.rownames = "ensembl")
resOrdered_LPS <- left_join(resOrdered_LPS, gencode_30, by = "ensembl")
resOrdered_LPS_top = resOrdered_LPS[order(resOrdered_LPS$padj) ,]
setDT(resOrdered_LPS_top, keep.rownames = "ensembl")
resOrdered_LPS_top = resOrdered_LPS_top[, c("ensembl", "symbol", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj")]
createDT(resOrdered_LPS_top)
write.table(resOrdered_LPS_top, "DEG_LPS_GTS.txt")

DESeq2: IFNy vs unstim

Number of differentially expressed genes

# generate results table for IFNy vs unstim
res_IFNy <- results(dds, name="Stimulation_IFNy_vs_unstim")
sum(res_IFNy$padj < 0.05, na.rm=TRUE)

[1] 62

resOrdered_IFNy <- res_IFNy[order(res_IFNy$pvalue),] 
resOrdered_IFNy <- as.data.frame(resOrdered_IFNy)

Volcano plot IFNy vs unstim

head(res_IFNy)

log2 fold change (MLE): Stimulation IFNy vs unstim Wald test p-value: Stimulation IFNy vs unstim DataFrame with 6 rows and 6 columns baseMean log2FoldChange lfcSE stat pvalue ENSG00000000003.14 21.3789 0.0663718 0.404489 0.164088 0.8696619 ENSG00000000419.12 216.0836 0.2944245 0.298930 0.984926 0.3246603 ENSG00000000457.14 98.3586 0.3680668 0.258796 1.422225 0.1549609 ENSG00000000460.17 29.1474 -0.5424023 0.310089 -1.749180 0.0802598 ENSG00000000938.13 583.1242 0.4613775 0.315087 1.464288 0.1431152 ENSG00000000971.15 48.0277 -0.0475215 0.351595 -0.135159 0.8924858 padj ENSG00000000003.14 0.999898 ENSG00000000419.12 0.999898 ENSG00000000457.14 0.999898 ENSG00000000460.17 0.999898 ENSG00000000938.13 0.999898 ENSG00000000971.15 0.999898

with(res_IFNy, plot(log2FoldChange, -log10(pvalue), pch=20, main="Volcano plot", xlim=c(-10,10)))
with(subset(res_IFNy, padj<.05 ), points(log2FoldChange, -log10(pvalue), pch=20, col="red"))

MA plot IFNy vs unstim

plotMA(res_IFNy, ylim=c(-2,2))

TOP differentially expressed genes IFNy vs unstim

setDT(resOrdered_IFNy, keep.rownames = "ensembl")
resOrdered_IFNy <- merge(resOrdered_IFNy, gencode_30, by = "ensembl")
resOrdered_IFNy_top = resOrdered_IFNy[order(resOrdered_IFNy$padj) ,]
setDT(resOrdered_IFNy_top, keep.rownames = "ensembl")
resOrdered_IFNy_top = resOrdered_IFNy_top[, c("ensembl", "symbol", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj")]
createDT(resOrdered_IFNy_top)
write.table(resOrdered_IFNy_top, "DEG_IFNy_GTS.txt")

DESeq2: R848 vs unstim

#only 1 sample is included, so removed stimulation from further analysis. ### Number of differentially expressed genes

# generate results table for R848 vs unstim
res_R848 <- results(dds, name="Stimulation_R848_vs_unstim")
sum(res_R848$padj < 0.05, na.rm=TRUE)

[1] 0

resOrdered_R848 <- res_R848[order(res_R848$pvalue),] 
resOrdered_R848 <- as.data.frame(resOrdered_R848)

Volcano plot R848 vs unstim

head(res_R848)

log2 fold change (MLE): Stimulation R848 vs unstim Wald test p-value: Stimulation R848 vs unstim DataFrame with 6 rows and 6 columns baseMean log2FoldChange lfcSE stat pvalue ENSG00000000003.14 21.3789 0.421613 1.128978 0.373447 0.7088161 ENSG00000000419.12 216.0836 1.577863 0.849002 1.858492 0.0630991 ENSG00000000457.14 98.3586 -0.598567 0.756988 -0.790721 0.4291066 ENSG00000000460.17 29.1474 -1.436813 0.955495 -1.503738 0.1326488 ENSG00000000938.13 583.1242 -0.749068 0.904824 -0.827860 0.4077498 ENSG00000000971.15 48.0277 0.928263 0.970448 0.956530 0.3388043 padj ENSG00000000003.14 0.999719 ENSG00000000419.12 0.939909 ENSG00000000457.14 0.999719 ENSG00000000460.17 0.988280 ENSG00000000938.13 0.999719 ENSG00000000971.15 0.999719

with(res_R848, plot(log2FoldChange, -log10(pvalue), pch=20, main="Volcano plot", xlim=c(-10,10)))
with(subset(res_R848, padj<.05 ), points(log2FoldChange, -log10(pvalue), pch=20, col="red"))

MA plot R848 vs unstim

plotMA(res_R848, ylim=c(-2,2))

TOP differentially expressed genes R848 vs unstim

setDT(resOrdered_R848, keep.rownames = "ensembl")
resOrdered_R848 <- left_join(resOrdered_R848, gencode_30, by = "ensembl")
resOrdered_R848_top = resOrdered_R848[order(resOrdered_R848$padj) ,]
setDT(resOrdered_R848_top, keep.rownames = "ensembl")
resOrdered_R848_top = resOrdered_R848_top[, c("ensembl", "symbol", "baseMean", "log2FoldChange", "lfcSE", "stat", "pvalue", "padj")]
createDT(resOrdered_R848_top)
write.table(resOrdered_R848_top, "DEG_R848_GTS.txt")

Create genelists with Log2FC < 1 and < -1 for different stimuli

resOrdered_LPS_p <- subset(resOrdered_LPS_top, padj < 0.05)
resOrdered_LPS_LFC <- subset(resOrdered_LPS_p, log2FoldChange > 1 | log2FoldChange < -1)
write.table(resOrdered_LPS_LFC, "LPS_GTS_FC1.txt")

resOrdered_IFNy_p <- subset(resOrdered_IFNy_top, padj < 0.05)
resOrdered_IFNy_LFC <- subset(resOrdered_IFNy_p, log2FoldChange > 1 | log2FoldChange < -1)
write.table(resOrdered_IFNy_LFC, "IFNy_GTS_FC1.txt")